package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.SomCommentMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.SomKnowledgeMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.SomKnowledgeTagMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.PraiseMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.Comment;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.Knowledge;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.Praise;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.SomKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 知识库service实现类
 * Created by liuyj on 2017/1/6.
 */
public class SomKnowledgeServiceImpl implements SomKnowledgeService {
    @Autowired
    private SomKnowledgeMapper somKnowledgeMapper;
    @Autowired
    private SomKnowledgeTagMapper somKnowledgeTagMapper;
    @Autowired
    private SomCommentMapper somCommentMapper;
    @Autowired
    private PraiseMapper praiseMapper;

    /**
     * 根据ID查询未删除知识 查看知识详情
     *
     * @param knowledgeId
     * @return
     */
    @Override
    public Knowledge findKnowledgeById(String knowledgeId, String userId) {
        // 创建知识对象
        Knowledge knowledge = new Knowledge();
        // 设置知识ID
        knowledge.setKnowledgeId(knowledgeId);
        knowledge.setCreatUserId(userId);
        knowledge.setLimit(1);
        knowledge.setOffset(0);
        // 执行操作并返回
        List<Knowledge> list = somKnowledgeMapper.selectKnowledgeBycondition(knowledge);
        if (list == null || list.size() < 1) {
            return null;
        } else {
            return list.get(0);
        }
    }

    /**
     * 根据文本内容模糊查询  标题 内容  标签
     *
     * @param knowledge
     * @return
     */
    @Override
    public List<Knowledge> findKnowledgeByCont(Knowledge knowledge) {
        return somKnowledgeMapper.selectKnowledgeBycondition(knowledge);
    }

    /**
     * 获取5个热门关键字
     *
     * @return
     */
    @Override
    public List<String> findKnowledgeTag() {
        return somKnowledgeTagMapper.findKnowledgeTag();
    }

    /**
     * 获取知识库评论
     *
     * @param comment
     * @return
     */
    @Override
    public List<Comment> getComment(Comment comment) {
        return somCommentMapper.getComment(comment);
    }

    /**
     * 添加评论
     *
     * @param comment
     * @return
     */
    @Override
    public int addComment(Comment comment) {
        comment.setCommentId(UniqueUtil.uuid());
        return somCommentMapper.addComment(comment);
    }

    /**
     * 点赞/取消点赞
     *
     * @param knowledgeId
     * @param userId
     * @return
     */
    @Override
    public void praise(String knowledgeId, String userId) {
        Praise praise = new Praise();
        praise.setKnowledgeId(knowledgeId);
        praise.setCreatUserId(userId);
        List<Praise> list = praiseMapper.queryPraise(praise);
        // 对应用户已经评论了知识库，取消点赞
        if (list != null && list.size() > 0) {
            praiseMapper.deletePraise(praise);
        } else {
            // 没有评论则是点赞
            praise.setPraiseId(UniqueUtil.uuid());
            praiseMapper.insert(praise);
        }
    }
}
